﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class adminstrator_findclassroom : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sconnectionstring = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sconnectionstring);
        myConnection.Open();
        if (Session["UserName"] == null && Session["UserNo"] == null)
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登录！'); parent.location.href='../login.aspx'</script>", false);
    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Session["sDL"] = "";
            Session["sTB"] = TextBox1.Text.Trim();
            if (DropDownList1.SelectedIndex == 0)
                Session["sDL"] = "rNo";
            else if (DropDownList1.SelectedIndex == 1)
                Session["sDL"] = "sks";
            else if (DropDownList1.SelectedIndex == 2)
                Session["sDL"] = "rDate";
            bind(Session["sDL"].ToString(), Session["sTB"].ToString());
        }
    }
    protected void ButtonReset_Click(object sender, EventArgs e)
    {
        DropDownList1.SelectedIndex = 0;
        TextBox1.Text = "";
        Gridview1.DataSource = null;
        label2.Text = "";
        Gridview1.DataBind();
    }
    protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Gridview1.PageIndex = e.NewPageIndex;
        bind(Session["sDL"].ToString(), Session["sTB"].ToString());
    }

    public void bind(string seField, string seValue)
    {
        string mysql = "select * from RoomTB where " + seField + " like '%" + seValue + "%' ";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataAdapter Adapter = new OleDbDataAdapter();
        Adapter.SelectCommand = mycmd;
        DataSet myDS = new DataSet();
        Adapter.Fill(myDS);
        if (myDS.Tables.Count > 0 && myDS.Tables[0].Rows.Count > 0)
        {
            Gridview1.DataSource = myDS;
            Gridview1.DataKeyNames = new string[] { "rNo", "sks", "sjs", "srs", "sclass", "sjsyt", "rDate" };
            Gridview1.DataBind();
            label2.Text = "";
        }
        else
        {
            label2.Text = "无法查询到相关数据";
            Gridview1.DataSource = null;
            Gridview1.DataBind();
        }

    }
}